package com.aiwiown.snackmq.console.web;

import com.aiwiown.snackmq.common.routing.ExchangeType;
import com.aiwiown.snackmq.console.service.SnackMQAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Slf4j
@Controller
@RequestMapping("/exchanges")
public class ExchangeController {

    @Autowired
    private SnackMQAdminService adminService;
    @GetMapping
    public String exchangePage(Model model) {
        try {
            // 为绑定表单的下拉列表获取所有 Topic
            model.addAttribute("topics", adminService.listTopics());
            model.addAttribute("exchanges", adminService.listExchanges());
        } catch (Exception e) {
            model.addAttribute("error", "Failed to fetch topic list for binding form: " + e.getMessage());
        }
        return "exchanges"; // 对应 exchanges.html
    }

    @PostMapping("/declare")
    public String declareExchange(@RequestParam String exchangeName,
                                  @RequestParam ExchangeType exchangeType,
                                  RedirectAttributes redirectAttributes) {
        try {
            adminService.declareExchange(exchangeName, exchangeType);
            redirectAttributes.addFlashAttribute("successMessage", "Exchange '" + exchangeName + "' declared successfully.");
        } catch (Exception e) {
            log.error("Failed to declare exchange '{}'", exchangeName, e);
            redirectAttributes.addFlashAttribute("errorMessage", "Failed to declare exchange: " + e.getMessage());
        }
        return "redirect:/exchanges";
    }

    @PostMapping("/bind")
    public String bindQueue(@RequestParam String queueName,
                            @RequestParam String exchangeName,
                            @RequestParam String routingKey,
                            RedirectAttributes redirectAttributes) {
        try {
            adminService.bindQueue(queueName, exchangeName, routingKey);
            redirectAttributes.addFlashAttribute("successMessage",
                    String.format("Queue '%s' bound to exchange '%s' with key '%s' successfully.", queueName, exchangeName, routingKey));
        } catch (Exception e) {
            log.error("Failed to bind queue '{}' to exchange '{}'", queueName, exchangeName, e);
            redirectAttributes.addFlashAttribute("errorMessage", "Failed to bind queue: " + e.getMessage());
        }
        return "redirect:/exchanges";
    }

    @PostMapping("/unbind")
    public String unbindQueue(@RequestParam String queueName,
                              @RequestParam String exchangeName,
                              @RequestParam String routingKey,
                              RedirectAttributes redirectAttributes) {
        try {
            adminService.unbindQueue(queueName, exchangeName, routingKey);
            redirectAttributes.addFlashAttribute("successMessage",
                    String.format("Binding for queue '%s' with key '%s' from exchange '%s' removed successfully.", queueName, routingKey, exchangeName));
        } catch (Exception e) {
            log.error("Failed to unbind queue '{}' from exchange '{}'", queueName, exchangeName, e);
            redirectAttributes.addFlashAttribute("errorMessage", "Failed to unbind queue: " + e.getMessage());
        }
        return "redirect:/exchanges";
    }

    @PostMapping("/delete")
    public String deleteExchange(@RequestParam String exchangeName,
                                 RedirectAttributes redirectAttributes) {
        try {
            adminService.deleteExchange(exchangeName);
            redirectAttributes.addFlashAttribute("successMessage",
                    String.format("Exchange '%s' deleted successfully.", exchangeName));
        } catch (Exception e) {
            log.error("Failed to delete exchange '{}'", exchangeName, e);
            redirectAttributes.addFlashAttribute("errorMessage", "Failed to delete exchange: " + e.getMessage());
        }
        return "redirect:/exchanges";
    }
}